package com.dongqi.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TComment;
import com.dongqi.myblog.mapper.TCommentMapper;
import com.dongqi.myblog.service.TCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Service
public class TCommentServiceImpl extends ServiceImpl<TCommentMapper, TComment> implements TCommentService {

    //存放所有子评论的集合
    private List<TComment> tempReplys = new ArrayList<>();

    /**
     * 根据blog_id获取该blog下的所有评论(需要进行评论扁平化处理)
     */
    @Override
    public List<TComment> getComments(Page<TComment> page,Long blogId) {
        QueryWrapper<TComment> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.blog_id",blogId)
                .eq("t1.parent_comment_id",0);
        //获取所有该blog下的一级评论
        List<TComment> comments = this.baseMapper.getComments(page, wrapper);
        return combineChildren(getTree(comments));
    }

    /**
     * 获取完整评论树
     * @param comments
     * @return
     */
    private List<TComment> getTree(List<TComment> comments){
        for(TComment c:comments){
            QueryWrapper<TComment> wrapper = new QueryWrapper<>();
            wrapper.eq("t1.parent_comment_id",c.getId());
            List<TComment> childComments = this.baseMapper.getComments(null, wrapper);
            if (childComments!=null&&childComments.size()!=0){
                c.setReplayComments(childComments);
                getTree(childComments);
            }
        }
        return comments;
    }

    /**
     * 保存评论信息（一条）
     */
    @Override
    @Transactional
    public boolean saveComment(TComment comment) {
        return this.save(comment);
    }

    /**
     * @param comments 一级评论的集合
     */
    private List<TComment> combineChildren(List<TComment> comments) {
        for (TComment comment : comments) {
            List<TComment> replys1 = comment.getReplayComments();
            for(TComment reply1 : replys1) {
                //循环迭代，找出子代，存放在tempReplys中
                recursively(reply1);
            }
            //修改顶级节点的reply子评论的集合为所有子评论的集合
            comment.setReplayComments(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }
        return comments;
    }

    /**
     * 递归迭代，剥洋葱
     * @param comment 被迭代的对象
     * @return
     */
    private void recursively(TComment comment) {
        tempReplys.add(comment);//顶节点添加到临时存放集合
        if (comment.getReplayComments().size()>0) {
            List<TComment> replys = comment.getReplayComments();
            for (TComment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplayComments().size()>0) {
                    recursively(reply);
                }
            }
        }
    }

}
